package controllerLayer;
import java.util.List;

import modelLayer.*;
import BDLayer.*;

public class StatusController {
	private ProController proCtr = new ProController();
	private StoreController storeCtr = new StoreController();
	private Status status;	
	private IFDBStatus statusDB = new DBStatus();
	private IFDBPartStatus partStatusDB = new DBPartStatus();
	private PersonController pCtr = new PersonController();
	private IFDBProductionEmployeeStatus proEmpSDB = new DBProductionEmployeeStatus();







	public Status getStatus(){
		return status;
	}

	public List<PartStatus> getAllPartStatus(){
		return status.getAllPartStatus();
	}



	//Interaction 1 	
	public void createStatus()
	{
		status = new Status(); 
		insertStatus();
	}


	//interaction 2

	//get all ProductionEmployees
	public List<Person> getAllProductionEmployee(){
		return proCtr.getAllProductionEmployee();
	}



	//set person on status object
	public void setPerson(List<String> list_p) throws Exception {
		List<Person> pList = pCtr.insertPerson(list_p);
		if(status != null){
			status.setListPerson(pList);

			p("status er ikke null  ");
		}
		else{
			p("status er null!!??  ");	
		}

		try{	
			proEmpSDB.deleteProductionEmployeeStatus(status);
			proEmpSDB.insertProductionEmployeeStatus(status);			

		}
		catch(Exception e){				
			new Exception("-personer er ikke gemt, noget gik galt");
		}	

	}






	//interaction 3
	public int setNightOrDay(int dayOrNight)
	{
		int rc = -1;
		if(status != null){
			status.setDayOrNight(dayOrNight);

			try {

				rc = statusDB.updateStatus(status);

			} catch (Exception e) {
				// TODO Auto-generated catch block

			}
		}
		return rc;
	}


	//interaction 4

	//get all Products
	public List<Product> getAllProduct(){
		return storeCtr.getAllProduct();
	}

	//create PartStatus add PartStatus to Status, and add Product to the PartStatus 
	public void setProduct(String prID){		
		if(status != null){
			PartStatus psObj = addPartStatus();
			
			Product prObj = storeCtr.findProductByID(prID);
			
			if(prObj != null){
				psObj.setProduct(prObj);

				try{
					partStatusDB.insertPartStatus(status, psObj, false);		
				}
				catch(Exception e){
					p("Exception : "+e);
				}
			}
		}



	}



	//interaction 5
	public List<Pallet> getAllPalletByProductID(String productID){
		return storeCtr.getAllPalletByProductID(productID);
	}





	//set Pallet on PartStatus
	public void setPallet(int psID, int palletID){
		Pallet paObj = storeCtr.findPalletByID(palletID);
		if(status != null){
			if(!status.getAllPartStatus().isEmpty()){
				PartStatus psObj = status.getPartStatus(psID);
				if(psObj != null){
					psObj.setPallet(paObj);
					
					updatePartStatus(psObj);
					
				}
			}
		}
	}


	public int updatePartStatus(PartStatus psObj){
		
		int rc = -1;
		try {
			rc = partStatusDB.updatePartStatus(status, psObj, false);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return rc;
	}



	//interaction 6,7,8  needs some adjusting in the modellayer
	public void updateOwnFail(int psID, int ownFail)
	{
		PartStatus psObj = getPartStatusByID(psID);		
		psObj.updateOwnFail(ownFail);
		updatePartStatus(psObj);
	}

	public void updateMachineFail(int psID, int machineFail)
	{
		PartStatus psObj = getPartStatusByID(psID);
		psObj.updateMachineFail(machineFail);
		updatePartStatus(psObj);
	}

	public void updatePastOver(int psID, int pastOver)
	{
		PartStatus psObj = getPartStatusByID(psID);
		psObj.updatePastOver(pastOver);
		updatePartStatus(psObj);
	}
	
	public void updateStatusDay(int psID, int statusDay){
		PartStatus psObj = getPartStatusByID(psID);
		psObj.updateStatusDay(statusDay);
		updatePartStatus(psObj);
	}


	//get PartStatus by ID
	public PartStatus getPartStatusByID(int psID){
		PartStatus psObj = null;
		if(status != null){
			psObj = status.getPartStatus(psID);

		}
		return psObj;
	}






	//interaction 9 	
	public String getComment(int psID){	
		
		PartStatus psObj = getPartStatusByID(psID);	
		
		psObj.getComments();
		return psObj.getComments();
	}

	public void setComment(int psID, String comment){
		PartStatus psObj = getPartStatusByID(psID);
		psObj.setComments(comment);
		updatePartStatus(psObj);
	}




	//interaction 10
	public void updateSupplierfail(int psID, int supplierFail)
	{
		PartStatus psObj = getPartStatusByID(psID);
		psObj.setSupplierFail(supplierFail);
		updatePartStatus(psObj);
		
	}
	
	public void updateNoOfPlates(int psID, int noOfPlates){
		PartStatus psObj = getPartStatusByID(psID);
		psObj.setFinishNumberOfPlates(noOfPlates);		
		updatePartStatus(psObj);
	}



	//interaction 11
	public void setFinishNoPlates(int psID, int finishNoOfPlates) {
		PartStatus psObj = getPartStatusByID(psID);
		psObj.setFinishNumberOfPlates(finishNoOfPlates);		
		updatePartStatus(psObj);		
	}


	//controlLayer for PartStatus



	public PartStatus addPartStatus()
	{		
		PartStatus ps = new PartStatus();
		if(status != null)
		{
			status.addPartStatus(ps);			
		}
		return ps;
	}


	public void removePartStatus(int psID){
		PartStatus psObj = getPartStatusByID(psID);		
		status.removePartStatus(psID);
		
		try {
			partStatusDB.deletePartStatus(psObj, false);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}





	//update pallet number of plates
	public boolean updatePallet(int psID){
		PartStatus psObj = getPartStatusByID(psID);	
		Pallet paObj = psObj.getPallet();
		return storeCtr.updatePallet(paObj);
	}
	
	



	public int insertAll(int i){
		
		status.generateFinishDate();
		int rc = -1;

		if(i == 0){
			rc = rc + updateStatus();
		}
		if(i == 1 && rc != -1){
			rc = updateAllPartStatus();
		}
		return rc;
	}

	
	public int insertStatus()
	{
		int rc = -1;
		try{
			rc = statusDB.insertSatus(status, false);
		}
		catch(Exception e){
			p("Exception insertStatus : "+e);
		}

		return rc;

	}

	public int updateStatus()
	{
		int rc = -1;
		try{
			rc = statusDB.updateStatus(status);
		}
		catch(Exception e){
			p("Exception insertStatus : "+e);
		}

		return rc;

	}

	public int deleteAll(){
		int rc = -1;
		try{
			if(!status.getAllPartStatus().isEmpty()){
				partStatusDB.deletePartStatusByStatus(status, false);					
			}	
			
			proEmpSDB.deleteProductionEmployeeStatus(status);
			rc = statusDB.deleteStatus(status);

		}
		catch(Exception e){
			p("Exception insertStatus : "+e);
		}

		return rc;
	}



	public int updateAllPartStatus(){
		int rc = -1;
		try{
			for(PartStatus ps : status.getAllPartStatus()){				
				partStatusDB.updatePartStatus(status, ps, false);
				updatePallet(ps.getPallet().getPalletID());
			}
		}
		catch(Exception e){

		}
		return rc;
	}


	//Person controls

	//return List of Person by type as parameter
	public List<Person> getAllPersonByType(String type, boolean retriveAssociation){
		return pCtr.getAllPersonByType(type, retriveAssociation);
	}







	public void p(String e){System.out.println(e);}








}
